﻿

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Data;
using System.Text;
using System.Xml;
using Gift.WebApp;
using Gift.Model;
using Gift.DAL;

namespace WebApp.admin.message
{
    public partial class messageCustomerService : BaseAdmin
    {
        // Fields
        tbCustomerService oCustomerService = new tbCustomerService();
       
        protected string xmlPath = "/cn/xml/customer_service.xml";

        // Methods
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                XmlDocument document = new XmlDocument();
                document.Load(base.Server.MapPath(this.xmlPath));
                XmlElement element = (XmlElement)document.SelectSingleNode("CustomerService");
                element.SetAttribute("title", this.txtCustomerServiceTitle.Value);
                element.SetAttribute("isShow", this.rdIsState1.Checked ? "true" : "false");
                document.Save(base.Server.MapPath(this.xmlPath));
                DataTable customerServiceByCompanyId = BaseFactory.theCustomerService.GetCustomerServiceByCompanyId(int.Parse(base.Administrator.CompanyId));
                ArrayList list = new ArrayList();
                if (base.Request.Form["hdCustomerService"] != null)
                {
                    list = ArrayList.Adapter(base.Request.Form["hdCustomerService"].Split(new char[] { ',' }));
                }
                foreach (DataRow row in customerServiceByCompanyId.Rows)
                {
                    if (!list.Contains(row["id"].ToString()))
                    {
                        BaseFactory.theCustomerService.Delete(Convert.ToInt32(row["id"]));
                    }
                }
                if (base.Request.Form["customerServiceType"] != null)
                {
                    ArrayList list2 = ArrayList.Adapter(base.Request.Form["customerServiceType"].Split(new char[] { ',' }));
                    ArrayList list3 = ArrayList.Adapter(base.Request.Form["title"].Split(new char[] { ',' }));
                    ArrayList list4 = ArrayList.Adapter(base.Request.Form["customerServiceNo"].Split(new char[] { ',' }));
                    this.oCustomerService.CompanyId = int.Parse(base.Administrator.CompanyId);
                    for (int i = 0; i < list2.Count; i++)
                    {
                        this.oCustomerService.customerServiceType = Convert.ToInt32(list2[i]);
                        this.oCustomerService.customerServiceNo = list4[i].ToString();
                        this.oCustomerService.title = list3[i].ToString();
                        this.oCustomerService.id = Convert.ToInt32(list[i]);
                        if (this.oCustomerService.id != 0)
                        {
                            BaseFactory.theCustomerService.Update(this.oCustomerService);
                        }
                        else
                        {
                            BaseFactory.theCustomerService.Add(this.oCustomerService);
                        }
                    }
                }
                base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新成功！\");location.href=\"messageCustomerService.aspx\";</script>");
            }
            catch
            {
            }
        }

        private void CustomerServiceBind()
        {
            try
            {
                XmlDocument document = new XmlDocument();
                document.Load(base.Server.MapPath(this.xmlPath));
                XmlElement element = (XmlElement)document.SelectSingleNode("CustomerService");
                this.txtCustomerServiceTitle.Value = element.GetAttribute("title");
                this.rdIsState1.Checked = element.GetAttribute("isShow") == "true";
                this.rdIsState0.Checked = element.GetAttribute("isShow") != "true";
            }
            catch
            {
            }
        }

        protected StringBuilder GetCustomerService()
        {
            StringBuilder builder = new StringBuilder();
            try
            {
               
                DataTable customerServiceByCompanyId = BaseFactory.theCustomerService.GetCustomerServiceByCompanyId(int.Parse(base.Administrator.CompanyId));
                builder.AppendLine("<table border=\"0\" cellspacing=\"0\" id=\"CustomerService\">");
                foreach (DataRow row in customerServiceByCompanyId.Rows)
                {
                    string str = "";
                    switch (Convert.ToInt32(row["customerServiceType"]))
                    {
                        case 0:
                            str = "QQ：";
                            break;

                        case 1:
                            str = "MSN：";
                            break;

                        case 2:
                            str = "旺旺：";
                            break;

                        case 3:
                            str = "Skype：";
                            break;
                    }
                    builder.AppendLine(string.Concat(new object[] { "<tr><td>", str, "<input name=\"hdCustomerService\" type=\"hidden\" value=\"", row["id"], "\" /><input name=\"customerServiceType\" type=\"hidden\" value=\"", row["customerServiceType"], "\" /></td><td><input name=\"customerServiceNo\" type=\"text\" value=\"", row["customerServiceNo"], "\" /> 标题：<input name=\"title\" type=\"text\" value=\"", row["title"], "\" /><input type=\"button\" class=\"formButton\" value=\"-\" title=\"删除客服\" onclick=\"delCustomerService(this.parentNode.parentNode.rowIndex)\"></td></tr>" }));
                }
                builder.AppendLine("</table>");
            }
            catch
            {
            }
            return builder;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            //if ((HttpContext.Current.Session["admin_user"] == null) && (HttpContext.Current.Session["language_version"] == null))
            //{
            //    base.ClientScript.RegisterStartupScript(base.GetType(), "sJs", "top.location=\"login.html\";", true);
            //}
            if (!base.IsPostBack)
            {
                this.CustomerServiceBind();
            }
        }
    }
}